# -*- coding: utf-8 -*-
from scrapy import FormRequest, Spider, cmdline, Request
from Phoneband.untils.utils_log import log, log_out
import json


class HuaweiSpider(Spider):
    name = 'huawei'
    allowed_domains = ['vmall.com']
    # start_urls = ['https://openapi.vmall.com/mcp/v1/search/queryPrd']
    start_urls = ['https://www.vmall.com']
    custom_settings = {
        'DEFAULT_REQUEST_HEADERS': {
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
            "Accept-Encoding": "gzip, deflate, br",
            "Accept-Language": "en,zh-CN;q=0.9,zh;q=0.8",
            "Cache-Control": "max-age=0",
            "Connection": "keep-alive",
            # "Cookie": "euid=bdba2a7f2aebb7b4df66f49f53ba81816f2fb270127775a7; CSRF-TOKEN=A839FEE54F2BE5A1AD230CC7D5507D3F0A8CEC1D9EB3E5D7; cps_id=119884; cps_direct=1; deviceid=b0133a654690f44cdf481e94971e29c6; TID=b0133a654690f44cdf481e94971e29c6; salePortal=1; HWWAFSESID=80d291305302609539a; HWWAFSESTIME=1604717329791; _areacode=CN; locale_MS=zh_CN; cps_track_id=1604717351723_m5rgpcg1jl",
            # "Host": "openapi.vmall.com",
            # "CsrfToken": "A839FEE54F2BE5A1AD230CC7D5507D3F0A8CEC1D9EB3E5D7",
            # "Upgrade-Insecure-Requests": "1",
        }
    }

    def parse(self, response):
        log('parse', response, type(response))
        log('headers', response.headers)
        cookie = response.headers.getlist('Set-Cookie')
        log('cookie', cookie)
        request_body = {
            'keyword': 36,
            'pageSize': 50,
            'pageNum': 1,
            'class_id': 0,
            'id': 0,
            'item': [],
        }
        yield Request(
            url='https://www.vmall.com/system/queryStatus.json',
            # meta={'item': cookie},
            callback=self.parse_cookie,
            meta={'data': request_body}
        )
        # with open('phone.html', 'w', encoding='utf-8') as f:
        #     f.write(response.text)

    def parse_cookie(self, response):
        request_body = response.meta['data']
        log('request_body_first', request_body, type(request_body))
        cookie2 = response.headers.getlist('Set-Cookie')
        # log('cookie1', cookie1)
        log('cookie2', cookie2)
        cookie = 'euid=' + str(cookie2).split('euid=')[1].split(';')[0]
        tooken = str(cookie2).split('CSRF-TOKEN=')[1].split(';')[0]
        log('cookie', cookie)
        # log('cookie_end', cookie_end)
        log('tooken', tooken)
        request_body['cookie'] = cookie
        request_body['token'] = tooken
        headers = {
            "Cookie": cookie,
            "CsrfToken": tooken,
        }
        # products_class = {
        #     '36': '261',   #手机
        #     '676': '262',  #笔记本
        #     '678': '262',  #平板
        #     '59': '261',  #智能穿戴设备&VR
        #     '714': '262',  #智能家居
        #     '646': '262',  #智慧屏
        #     '652': '262',  #耳机音响
        #     '54': '261',  #配件
        #     '43': '261',  #生态产品
        #     '34': '261',  #增值服务&其他
        # }
        data = {
                'brandType': '0',
                'country': "CN",
                'keyword': request_body['keyword'],
                # 'keyword': keyword,
                'lang': "zh-CN",
                'pageNum': request_body['pageNum'],
                'pageSize': request_body['pageSize'],
                'portal': '1',
                'screenParams': "{}",
                'searchFlag': '1',
                'searchId': "bb59928b82faf84786883a18a04e411c",
                'searchSortField': '0',
                'searchSortType': "desc",
                'tid': "bb59928b82faf84786883a18a04e411c",
            }
        form_data = json.dumps(data)
        log('form_data', form_data, type(form_data))
        # yield FormRequest(
        #     url='https://openapi.vmall.com/mcp/v1/search/queryPrd',
        #     body=form_data,
        #     # cookies=cookie_end,
        #     headers=headers,
        #     callback=self.parse_data
        # )
        yield Request(
            url='https://openapi.vmall.com/mcp/v1/search/queryPrd',
            body=form_data,
            # cookies=cookie_end,
            method="POST",
            headers=headers,
            callback=self.parse_data,
            meta={'data': request_body},
        )

    def parse_cookie2(self, response):
        request_body = response.meta['data']
        log('request_body_first', request_body, type(request_body))

        headers = {
            "Cookie": request_body['cookie'],
            "CsrfToken": request_body['token'],
        }
        # products_class = {
        #     '36': '261',   #手机
        #     '676': '262',  #笔记本
        #     '678': '262',  #平板
        #     '59': '261',  #智能穿戴设备&VR
        #     '714': '262',  #智能家居
        #     '646': '262',  #智慧屏
        #     '652': '262',  #耳机音响
        #     '54': '261',  #配件
        #     '43': '261',  #生态产品
        #     '34': '261',  #增值服务&其他
        # }
        data = {
                'brandType': '0',
                'country': "CN",
                'keyword': request_body['keyword'],
                # 'keyword': keyword,
                'lang': "zh-CN",
                'pageNum': request_body['pageNum'],
                'pageSize': request_body['pageSize'],
                'portal': '1',
                'screenParams': "{}",
                'searchFlag': '1',
                'searchId': "bb59928b82faf84786883a18a04e411c",
                'searchSortField': '0',
                'searchSortType': "desc",
                'tid': "bb59928b82faf84786883a18a04e411c",
            }
        form_data = json.dumps(data)
        log('form_data', form_data, type(form_data))
        # yield FormRequest(
        #     url='https://openapi.vmall.com/mcp/v1/search/queryPrd',
        #     body=form_data,
        #     # cookies=cookie_end,
        #     headers=headers,
        #     callback=self.parse_data
        # )
        yield Request(
            url='https://openapi.vmall.com/mcp/v1/search/queryPrd',
            body=form_data,
            # cookies=cookie_end,
            method="POST",
            headers=headers,
            callback=self.parse_data,
            meta={'data': request_body},
        )

    # def parse_total(self, response):
    #     body = response.text
    #     data = json.loads(body)
    #     total = data['']

    def parse_data(self, response):
        # log('response_header', response.headers)
        products_class = [
            '36',  # 手机
            '676',  # 笔记本
            '678',  # 平板
            '59',  # 智能穿戴设备&VR
            '714',  # 智能家居
            '646',  # 智慧屏
            '652',  # 耳机音响
            '54',  # 配件
            '43',  # 生态产品
            '34',  # 增值服务&其他
        ]
        class_num = len(products_class)

        request_body = response.meta['data']
        log('request_body_start', request_body, type(request_body))
        class_id = request_body['class_id']
        page_num = request_body['pageNum']
        id = request_body['id']
        page_size = request_body['pageSize']
        item = request_body['item']

        body = response.text
        data = json.loads(body)
        # log('data', data)
        phone = data['resultList']
        total = data['totalCount']
        if page_size * page_num >= total:
            if class_id < class_num - 1:

                for i in range(len(phone)):
                    block = {
                        'id': id,
                        'title': phone[i]['name'],
                        'url': 'https://www.vmall.com/product/' + str(phone[i]['productId']) + '.html',
                        'price': phone[i]['price'],
                        'imgs': '["https://res.vmallres.com/pimages/{}428_428_{}"]'.format(phone[i]['photoPath'], phone[i][
                        'photoName']),
                        'desc': phone[i]['skuName'],
                        'shop_name': '华为官方',

                    }
                    item.append(block)
                    id += 1
                log('item', item)

                result = json.dumps(item)
                log_out(result)
                # with open('huawei.json', 'a') as f:
                #     f.write(result)
                request_body['pageNum'] = 1
                request_body['class_id'] += 1
                request_body['keyword'] = products_class[class_id + 1]
                request_body['id'] = id
                request_body['item'] = []
                log('request_body1', request_body)
                yield Request(
                    url='https://www.vmall.com/system/queryStatus.json',
                    # meta={'item': cookie},
                    callback=self.parse_cookie2,
                    meta={'data': request_body},
                    dont_filter=True,
                )
            else:
                return item
        else:
            for i in range(len(phone)):
                block = {
                    'id': id,
                    'title': phone[i]['name'],
                    'url': 'https://www.vmall.com/product/' + str(phone[i]['productId']) + '.html',
                    'price': phone[i]['price'],
                    'imgs': '["https://res.vmallres.com/pimages/{}428_428_{}"]'.format(phone[i]['photoPath'], phone[i][
                        'photoName']),
                    'desc': phone[i]['skuName'],
                    'shop_name': '华为官方',
                }
                item.append(block)
                id += 1
            log('item', item)
            request_body['pageNum'] += 1
            request_body['id'] = id
            log('phone', phone)
            log('request_body2', request_body)
            yield Request(
                url='https://www.vmall.com/system/queryStatus.json',
                # meta={'item': cookie},
                callback=self.parse_cookie2,
                meta={'data': request_body},
                dont_filter=True,
            )


if __name__ == '__main__':
    cmdline.execute("scrapy crawl huawei".split())
